

var defaultValue = Cesium.defaultValue;
var SceneMode = Cesium.SceneMode;



function ComsatScanPrimitive(options) {
    options = defaultValue(options, defaultValue.EMPTY_OBJECT);

    this.show = defaultValue(options.show, true);

    this.angle = defaultValue(options.angle, 0);
    this._angle = 0;

    this.height = defaultValue(options.height, 0);
    this._height = 0;

    this.modelMatrix = Cesium.Matrix4.clone(options.modelMatrix, new Cesium.Matrix4());
    this._modelMatrix = new Cesium.Matrix4();

    this.bottomArray = defaultValue([], []);
    this._bottomArray = [];



    this.color = defaultValue(options.color, Cesium.Color.WHITE);
    this._color = Cesium.Color.WHITE;

    this.vs = `
    attribute vec4 position;
    attribute vec3 normal;
    
    varying vec3 v_position;
    varying vec3 v_positionWC;
    varying vec3 v_positionEC;
    varying vec3 v_normalEC;
    
    void main()
    {
        gl_Position = czm_modelViewProjection * position;
        v_position = vec3(position);
        v_positionWC = (czm_model * position).xyz;
        v_positionEC = (czm_modelView * position).xyz;
        v_normalEC = czm_normal * normal;
    }`;
    this.fs = this.renderFS();
}
ComsatScanPrimitive.prototype.renderFS = function () {
    return `void main(){gl_FragColor=vec4(${this._color.red},${this._color.green},${this._color.blue},${this._color.alpha});}`;
}
ComsatScanPrimitive.prototype.isDestroyed = function () {
    return false
}
ComsatScanPrimitive.prototype.destroy = function () {

}
ComsatScanPrimitive.prototype.update = function (frameState) {
    var mode = frameState.mode;
    if (!this.show || mode !== SceneMode.SCENE3D) {
        return;
    }
    var isRender = false;
    if (this.color) {
        if (!this.color.equals(this._color)) {
            isRender = true;
            this._color = this.color;
            this.fs = this.renderFS();
        };
    }
    if (this.modelMatrix != this._modelMatrix) {
        isRender = true;
        this._modelMatrix = this.modelMatrix;
    };
    if (this.height != this._height) {
        isRender = true;
        this._height = this.height;
    };

    if (this.angle != this._angle) {
        isRender = true;
        this._angle = this.angle;
        var obj = createVertexArray(this._angle, this._height);
        this._point = obj.point;
        this.bottomArray = obj.bottomArray;
    };
    var pointCommand = getCommand(this.vs, this.fs, frameState, this._point, this._modelMatrix);
    var commandList = frameState.commandList
    commandList.push(pointCommand);
}

var CX = function (radius, angle) {
    return radius * Math.cos(angle);
}
var CY = function (radius, angle) {
    return radius * Math.sin(angle);
}

var radian2angle = function (angle) {
    return angle * Math.PI / 180;
}

function createVertexArray(angle, height) {
    var radius = CY(5000000 + height, radian2angle(angle));
    var point = [];
    var bottomArray = [];
    for (let i = 0; i < 360; i += 90) {
        var radian = radian2angle(i)
        var next_radian = radian2angle(i + 90);
        point.push(0, 0, 0);
        point.push(CX(radius, radian), CY(radius, radian), -5000000 - height);
        bottomArray.push([CX(radius, radian), CY(radius, radian), -5000000 - height]);
        point.push(CX(radius, next_radian), CY(radius, next_radian), -5000000 - height);
    }
    return {
        bottomArray,
        point,
    }
}

//用prototype给定方法和属性
var getCommand = function (v, f, frameState, arr, modelMatrix) {
    var context = frameState.context;
    //shaderProgram将两个着色器合并
    var shaderProgram = Cesium.ShaderProgram.fromCache({
        context: context,
        vertexShaderSource: v,
        fragmentShaderSource: f
    })
    //渲染状态
    var renderState = Cesium.RenderState.fromCache({
        depthTest: {
            enabled: true
        },
        depthMask: false,
        blending: Cesium.BlendingState.ALPHA_BLEND
    })
    //顶点数组Buffer
    var vertexBuffer = Cesium.Buffer.createVertexBuffer({
        context: context,
        typedArray: Cesium.ComponentDatatype.createTypedArray(
            Cesium.ComponentDatatype.FLOAT,
            arr,
        ),
        usage: Cesium.BufferUsage.STATIC_DRAW
    })
    //用来表示逐个顶点的信息
    var attributes = []
    attributes.push({
        index: 0,
        vertexBuffer: vertexBuffer,
        componentDatatype: Cesium.ComponentDatatype.FLOAT,
        componentsPerAttribute: 3,
        normalize: false
    })
    //顶点数组（设置顶点属性和索引Buffer）
    var vertexArray = new Cesium.VertexArray({
        context: context,
        attributes: attributes,
    })

    //新建一个DrawCommand
    return new Cesium.DrawCommand({
        // 渲染线类型
        // primitiveType: Cesium.PrimitiveType.LINES,
        // 渲染面类型
        primitiveType: Cesium.PrimitiveType.TRIANGLES,
        shaderProgram: shaderProgram,
        renderState: renderState,
        vertexArray: vertexArray,
        pass: Cesium.Pass.OPAQUE,
        modelMatrix: modelMatrix,
    })
}


export { ComsatScanPrimitive }